package edu.scut.oop.exercise.v2;

import java.util.List;

/**
 * 习题生成器测试类
 * 测试ExerciseGenerator的所有功能
 */
public class ExerciseGeneratorTest {
    private static int passedTests = 0;
    private static int totalTests = 0;
    
    public static void main(String[] args) {
        System.out.println("🧪 开始运行习题生成器单元测试\n");
        
        try {
            // 基本功能测试
            testExerciseGeneratorCreation();
            testAdditionExercisesGeneration();
            testSubtractionExercisesGeneration();
            testMixedExercisesGeneration();
            testCompleteExerciseSetGeneration();
            testFormatDisplay();
            testExerciseValidation();
            testCounterManagement();
            
            // 输出测试结果
            System.out.println("\n" + "=".repeat(60));
            System.out.println("📊 测试结果汇总");
            System.out.println("=".repeat(60));
            System.out.println(String.format("✅ 通过测试: %d / %d", passedTests, totalTests));
            System.out.println(String.format("❌ 失败测试: %d / %d", totalTests - passedTests, totalTests));
            
            if (passedTests == totalTests) {
                System.out.println("\n🎉 所有测试通过！习题生成系统功能正常！");
            } else {
                System.out.println("\n⚠️ 有测试失败，需要检查代码实现。");
            }
            
        } catch (Exception e) {
            System.err.println("❌ 测试执行过程中发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    /**
     * 测试ExerciseGenerator的创建
     */
    private static void testExerciseGeneratorCreation() {
        test("ExerciseGenerator - 创建测试", () -> {
            ExerciseGenerator generator = new ExerciseGenerator();
            return generator != null;
        });
        
        test("ExerciseGenerator - 种子创建测试", () -> {
            ExerciseGenerator generator = new ExerciseGenerator(12345L);
            return generator != null;
        });
    }
    
    /**
     * 测试加法习题生成
     */
    private static void testAdditionExercisesGeneration() {
        test("ExerciseGenerator - 10道加法习题生成", () -> {
            ExerciseGenerator generator = new ExerciseGenerator(42L); // 使用固定种子确保可重现
            List<Exercise> exercises = generator.generateAdditionExercises(10);
            
            // 验证数量
            if (exercises.size() != 10) return false;
            
            // 验证都是加法算式
            for (Exercise exercise : exercises) {
                if (!(exercise.getExpression() instanceof AdditionExpression)) {
                    return false;
                }
            }
            
            return true;
        });
        
        test("ExerciseGenerator - 50道加法习题生成", () -> {
            ExerciseGenerator generator = new ExerciseGenerator(42L);
            List<Exercise> exercises = generator.generateAdditionExercises(50);
            return exercises.size() == 50;
        });
        
        test("ExerciseGenerator - 加法习题属性验证", () -> {
            ExerciseGenerator generator = new ExerciseGenerator(42L);
            List<Exercise> exercises = generator.generateAdditionExercises(5);
            
            Exercise firstExercise = exercises.get(0);
            
            // 验证习题编号从1开始
            if (firstExercise.getId() != 1) return false;
            
            // 验证有有效表达式
            if (firstExercise.getExpression() == null) return false;
            
            // 验证有难度级别
            if (firstExercise.getDifficulty() == null) return false;
            
            return true;
        });
    }
    
    /**
     * 测试减法习题生成
     */
    private static void testSubtractionExercisesGeneration() {
        test("ExerciseGenerator - 10道减法习题生成", () -> {
            ExerciseGenerator generator = new ExerciseGenerator(42L);
            List<Exercise> exercises = generator.generateSubtractionExercises(10);
            
            // 验证数量
            if (exercises.size() != 10) return false;
            
            // 验证都是减法算式
            for (Exercise exercise : exercises) {
                if (!(exercise.getExpression() instanceof SubtractionExpression)) {
                    return false;
                }
            }
            
            return true;
        });
        
        test("ExerciseGenerator - 50道减法习题生成", () -> {
            ExerciseGenerator generator = new ExerciseGenerator(42L);
            List<Exercise> exercises = generator.generateSubtractionExercises(50);
            return exercises.size() == 50;
        });
        
        test("ExerciseGenerator - 减法习题结果有效性", () -> {
            ExerciseGenerator generator = new ExerciseGenerator(42L);
            List<Exercise> exercises = generator.generateSubtractionExercises(20);
            
            for (Exercise exercise : exercises) {
                SubtractionExpression expr = (SubtractionExpression) exercise.getExpression();
                // 验证减法结果为正数
                if (expr.calculate() <= 0) return false;
            }
            
            return true;
        });
    }
    
    /**
     * 测试混合习题生成
     */
    private static void testMixedExercisesGeneration() {
        test("ExerciseGenerator - 10道混合习题生成", () -> {
            ExerciseGenerator generator = new ExerciseGenerator(42L);
            List<Exercise> exercises = generator.generateMixedExercises(10);
            
            // 验证数量
            if (exercises.size() != 10) return false;
            
            // 验证包含加法和减法
            boolean hasAddition = false;
            boolean hasSubtraction = false;
            
            for (Exercise exercise : exercises) {
                if (exercise.getExpression() instanceof AdditionExpression) {
                    hasAddition = true;
                } else if (exercise.getExpression() instanceof SubtractionExpression) {
                    hasSubtraction = true;
                }
            }
            
            return hasAddition && hasSubtraction;
        });
        
        test("ExerciseGenerator - 50道混合习题生成", () -> {
            ExerciseGenerator generator = new ExerciseGenerator(42L);
            List<Exercise> exercises = generator.generateMixedExercises(50);
            return exercises.size() == 50;
        });
    }
    
    /**
     * 测试完整习题集生成
     */
    private static void testCompleteExerciseSetGeneration() {
        test("ExerciseGenerator - 完整习题集生成", () -> {
            ExerciseGenerator generator = new ExerciseGenerator(42L);
            ExerciseSet exerciseSet = generator.generateCompleteExerciseSet();
            
            // 验证总题数
            if (exerciseSet.getTotalExercises() != 150) return false;
            
            // 验证习题集名称
            if (!exerciseSet.getName().equals("完整习题集")) return false;
            
            return true;
        });
        
        test("ExerciseGenerator - 习题集内容验证", () -> {
            ExerciseGenerator generator = new ExerciseGenerator(42L);
            ExerciseSet exerciseSet = generator.generateCompleteExerciseSet();
            
            List<Exercise> allExercises = exerciseSet.getAllExercises();
            
            // 验证习题编号连续性
            for (int i = 0; i < allExercises.size(); i++) {
                if (allExercises.get(i).getId() != i + 1) return false;
            }
            
            return true;
        });
    }
    
    /**
     * 测试格式化显示
     */
    private static void testFormatDisplay() {
        test("ExerciseGenerator - 格式化显示", () -> {
            ExerciseGenerator generator = new ExerciseGenerator(42L);
            List<Exercise> exercises = generator.generateAdditionExercises(5);
            String formatted = generator.formatExercises(exercises, "测试标题");
            
            // 验证包含关键内容
            if (!formatted.contains("测试标题")) return false;
            if (!formatted.contains("总题数: 5")) return false;
            if (!formatted.contains("001.")) return false;
            
            return true;
        });
        
        test("ExerciseGenerator - 完整习题集格式化", () -> {
            ExerciseGenerator generator = new ExerciseGenerator(42L);
            ExerciseSet exerciseSet = generator.generateCompleteExerciseSet();
            String formatted = generator.formatExerciseSet(exerciseSet);
            
            // 验证包含统计信息
            if (!formatted.contains("📊 统计信息")) return false;
            if (!formatted.contains("总题数: 150")) return false;
            if (!formatted.contains("📝 习题分类")) return false;
            
            return true;
        });
    }
    
    /**
     * 测试习题验证功能
     */
    private static void testExerciseValidation() {
        test("ExerciseGenerator - 生成的习题可回答", () -> {
            ExerciseGenerator generator = new ExerciseGenerator(42L);
            List<Exercise> exercises = generator.generateAdditionExercises(5);
            
            Exercise exercise = exercises.get(0);
            AdditionExpression expr = (AdditionExpression) exercise.getExpression();
            int correctAnswer = expr.calculate();
            
            // 提交正确答案
            boolean result = exercise.submitAnswer(correctAnswer);
            if (!result) return false;
            
            // 验证状态已更新
            return exercise.getStatus() == Exercise.Status.COMPLETED;
        });
        
        test("ExerciseGenerator - 错误答案处理", () -> {
            ExerciseGenerator generator = new ExerciseGenerator(42L);
            List<Exercise> exercises = generator.generateAdditionExercises(5);
            
            Exercise exercise = exercises.get(0);
            
            // 提交错误答案
            boolean result = exercise.submitAnswer(999);
            if (result) return false; // 应该返回false
            
            // 验证状态
            return exercise.getStatus() == Exercise.Status.INCORRECT;
        });
    }
    
    /**
     * 测试计数器管理
     */
    private static void testCounterManagement() {
        test("ExerciseGenerator - 计数器递增", () -> {
            ExerciseGenerator generator = new ExerciseGenerator(42L);
            generator.generateAdditionExercises(5);
            return generator.getExerciseCounter() == 6; // 5 + 1
        });
        
        test("ExerciseGenerator - 计数器重置", () -> {
            ExerciseGenerator generator = new ExerciseGenerator(42L);
            generator.generateAdditionExercises(10);
            generator.resetCounter();
            return generator.getExerciseCounter() == 1;
        });
    }
    
    /**
     * 执行单个测试
     * @param testName 测试名称
     * @param testFunction 测试函数
     */
    private static void test(String testName, TestFunction testFunction) {
        totalTests++;
        System.out.println("🧪 测试: " + testName);
        
        try {
            boolean result = testFunction.run();
            if (result) {
                System.out.println("✅ 通过");
                passedTests++;
            } else {
                System.out.println("❌ 失败");
            }
        } catch (Exception e) {
            System.out.println("❌ 异常: " + e.getMessage());
        }
        System.out.println();
    }
    
    /**
     * 测试函数接口
     */
    @FunctionalInterface
    interface TestFunction {
        boolean run() throws Exception;
    }
}